const {
  baseUrl,
  getRequest,
  postRequest
} = require("../../../utils/request.js")
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //账号密码
    username: '',
    password: '',
    //手机号
    phone: '',
    code: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },
  /**
   * 注册页面
   */
  onRegisterPage() {
    wx.navigateTo({
      url: '/pages/home/register/register',
    })
  },
  /**
   * 账号密码登录
   */
  submitLoginPwd() {
    if (this.data.username != '' && this.data.password != '') {
      postRequest(baseUrl + "/front/user/loginPassword", {
        username: this.data.username,
        password: this.data.password,
      }).then(res => {
        //console.log(res.data);
        if (res.code == 200) {
          getRequest(baseUrl + "/front/user/getUser", {
            token: res.data
          }).then(res => {
            //console.log(res.data)
            if (res.code == 200) {
              //保存用户信息
              wx.setStorage({
                key: "userId",
                data: res.data.userId
              })
              //跳转到首页
              wx.switchTab({
                url: '/pages/home/index/index',
              })
            }
          })
        }
      })
    } else {
      wx.showToast({
        title: '请输入账号和密码',
        icon: 'error',
        duration: 2000
      })
    }

  },
  /**
   * 手机号登录
   */
  submitLoginPhone() {
    console.log('手机号登录')
    //保存用户信息
    wx.setStorage({
      key: "userId",
      data: 1
    })
    //跳转到首页
    wx.switchTab({
      url: '/pages/home/index/index',
    })
  },
})